package com.lxl.xm.controller;

import com.lxl.xm.common.BaseResponse;
import com.lxl.xm.common.ErrorCode;
import com.lxl.xm.common.ResultUtils;
import com.lxl.xm.exception.BusinessException;
import com.lxl.xm.model.dto.student_user.StudentUserLoginRequest;
import com.lxl.xm.model.dto.student_user.StudentUserRegisterRequest;
import com.lxl.xm.model.entity.StudentUser;
import com.lxl.xm.model.entity.User;
import com.lxl.xm.service.StudentUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/studentUser")
@Slf4j
public class StudentUserController {

    @Resource
    StudentUserService studentUserService;

    /**
     * 学生用户注册
     * @param studentUserRegisterRequest
     * @return
     */
    @RequestMapping("/register")
    public BaseResponse<Long> studentRegister(@RequestBody StudentUserRegisterRequest studentUserRegisterRequest){
        if (studentUserRegisterRequest == null ){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"学生注册信息错误");
        }
        Long result = studentUserService.studentUserRegister(studentUserRegisterRequest);
        return ResultUtils.success(result);
    }


    @PostMapping("/login")
    public BaseResponse<StudentUser> studentUserLogin(@RequestBody StudentUserLoginRequest studentUserLoginRequest){
        if (studentUserLoginRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String studentNumber = studentUserLoginRequest.getStudentNumber();
        String studentPassword = studentUserLoginRequest.getStudentPassword();
        if (StringUtils.isAnyBlank(studentNumber, studentPassword)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        StudentUser studentUser = studentUserService.login(studentNumber, studentPassword);
        return ResultUtils.success(studentUser);
    }



}
